Micron Document
🎖️GitЯра🎖️


Displaying Raw • Download

androidApp/src/testGoogle/kotlin/org/meshtastic/app/map/offline/pmtiles/MvtDecoderTest.kt renovate/fastlane-2.x-lockfile (056eed64) Text, 6.69 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.app.map.offline.pmtiles

Tff7b72import T7ee787kotlinx.serialization.ExperimentalSerializationApi
Tff7b72import T7ee787kotlinx.serialization.protobuf.ProtoBuf
Tff7b72import T7ee787kotlin.test.Test
Tff7b72import T7ee787kotlin.test.assertEquals

T8b949e/**
* [MVT_TILE_BYTES] is a hand-encoded protobuf message, not a sample pulled from a real tile or built via
* [ProtoBuf.encodeToByteArray] — encoding it with the very serializer under test would only prove the encoder and
* decoder agree with each other, not that either agrees with the wire format real PMTiles data actually uses (in
* particular, whether `kotlinx-serialization-protobuf` decodes MVT's *packed* repeated `uint32` fields correctly, which
* is the one place this module leans on library behaviour it doesn't otherwise exercise). Encodes: `Tile{ layers:
* [ Layer{ name="l", version=2, extent=4096, features: [ Feature{ id=1, type=POLYGON, geometry=[MoveTo(0,0), LineTo(10,0), LineTo(10,10), LineTo(0,10), ClosePath]
* } ] } ] }`. See the field-by-field byte breakdown in a comment on each line below.
*/
Tf0883e@OptInTb4b4b4(Te6edf3ExperimentalSerializationApiTff7b72::Te6edf3classTb4b4b4)
Tff7b72private Tff7b72val Te6edf3MVT_TILE_BYTES Tff7b72=
Te6edf3byteArrayOfTb4b4b4(
T79c0ff0Te6edf3x1ATb4b4b4,
T79c0ff0Te6edf3x1BTb4b4b4, T8b949e// Tile.layers[0]: tag=3<<3|2, len=27
T79c0ff0Te6edf3x0ATb4b4b4,
T79c0ff0Te6edf3x01Tb4b4b4,
T79c0ff0Te6edf3x6CTb4b4b4, T8b949e// Layer.name = "l"
T79c0ff0Te6edf3x12Tb4b4b4,
T79c0ff0Te6edf3x11Tb4b4b4, T8b949e// Layer.features[0]: tag=2<<3|2, len=17
T79c0ff0Te6edf3x08Tb4b4b4,
T79c0ff0Te6edf3x01Tb4b4b4, T8b949e// Feature.id = 1
T79c0ff0Te6edf3x18Tb4b4b4,
T79c0ff0Te6edf3x03Tb4b4b4, T8b949e// Feature.type = POLYGON (3)
T79c0ff0Te6edf3x22Tb4b4b4,
T79c0ff0Te6edf3x0BTb4b4b4, T8b949e// Feature.geometry: tag=4<<3|2 (packed), len=11
T79c0ff0Te6edf3x09Tb4b4b4,
T79c0ff0Te6edf3x00Tb4b4b4,
T79c0ff0Te6edf3x00Tb4b4b4, T8b949e// MoveTo(count=1) -> zigzag(0), zigzag(0): (0,0)
T79c0ff0Te6edf3x1ATb4b4b4,
T79c0ff0Te6edf3x14Tb4b4b4,
T79c0ff0Te6edf3x00Tb4b4b4, T8b949e// LineTo(count=3), first pair -> zigzag(10)=20, zigzag(0)=0: +(10,0) = (10,0)
T79c0ff0Te6edf3x00Tb4b4b4,
T79c0ff0Te6edf3x14Tb4b4b4, T8b949e// second pair -> zigzag(0)=0, zigzag(10)=20: +(0,10) = (10,10)
T79c0ff0Te6edf3x13Tb4b4b4,
T79c0ff0Te6edf3x00Tb4b4b4, T8b949e// third pair -> zigzag(-10)=19, zigzag(0)=0: +(-10,0) = (0,10)
T79c0ff0Te6edf3x0FTb4b4b4, T8b949e// ClosePath(count=1)
T79c0ff0Te6edf3x28Tb4b4b4,
T79c0ff0Te6edf3x80Tb4b4b4.Te6edf3toByteTb4b4b4(Tb4b4b4)Tb4b4b4,
T79c0ff0Te6edf3x20Tb4b4b4, T8b949e// Layer.extent = 4096 (varint: 0x80 0x20)
T79c0ff0Te6edf3x78Tb4b4b4,
T79c0ff0Te6edf3x02Tb4b4b4, T8b949e// Layer.version = 2
Tb4b4b4)

Tff7b72class T56d364MvtDecoderTest Tb4b4b4{

Tf0883e@OptInTb4b4b4(Te6edf3ExperimentalSerializationApiTff7b72::Te6edf3classTb4b4b4)
Tf0883e@Test
Tff7b72fun Td2a8ff`decodes a hand-built tile's layer, feature and geometry fields`Tb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3tile Tff7b72= Te6edf3ProtoBufTb4b4b4.Te6edf3decodeFromByteArrayTb4b4b4(Te6edf3VectorTileTb4b4b4.Te6edf3serializerTb4b4b4(Tb4b4b4)Tb4b4b4, Te6edf3MVT_TILE_BYTESTb4b4b4)

Tff7b72val Te6edf3layer Tff7b72= Te6edf3tileTb4b4b4.Te6edf3layersTb4b4b4.Te6edf3singleTb4b4b4(Tb4b4b4)
Te6edf3assertEqualsTb4b4b4(Ta5d6ff"Ta5d6fflTa5d6ff"Tb4b4b4, Te6edf3layerTb4b4b4.Te6edf3nameTb4b4b4)
Te6edf3assertEqualsTb4b4b4(T79c0ff2Tb4b4b4, Te6edf3layerTb4b4b4.Te6edf3versionTb4b4b4)
Te6edf3assertEqualsTb4b4b4(T79c0ff4T79c0ff0T79c0ff9T79c0ff6Tb4b4b4, Te6edf3layerTb4b4b4.Te6edf3extentTb4b4b4)

Tff7b72val Te6edf3feature Tff7b72= Te6edf3layerTb4b4b4.Te6edf3featuresTb4b4b4.Te6edf3singleTb4b4b4(Tb4b4b4)
Te6edf3assertEqualsTb4b4b4(T79c0ff1LTb4b4b4, Te6edf3featureTb4b4b4.Te6edf3idTb4b4b4)
Te6edf3assertEqualsTb4b4b4(Te6edf3VectorTileTb4b4b4.Te6edf3GEOM_POLYGONTb4b4b4, Te6edf3featureTb4b4b4.Te6edf3typeTb4b4b4)
Tb4b4b4}

Tf0883e@Test
Tff7b72fun Td2a8ff`decodes a closed polygon ring from its MoveTo-LineTo-ClosePath command stream`Tb4b4b4(Tb4b4b4) Tb4b4b4{
T8b949e// The raw geometry ints from MVT_TILE_BYTES's geometry field, spelled out rather than re-parsed, so this
T8b949e// test still pins the exact command stream the byte comment above claims to encode.
Tff7b72val Te6edf3commands Tff7b72= Te6edf3listOfTb4b4b4(T79c0ff9Tb4b4b4, T79c0ff0Tb4b4b4, T79c0ff0Tb4b4b4, T79c0ff2T79c0ff6Tb4b4b4, T79c0ff2T79c0ff0Tb4b4b4, T79c0ff0Tb4b4b4, T79c0ff0Tb4b4b4, T79c0ff2T79c0ff0Tb4b4b4, T79c0ff1T79c0ff9Tb4b4b4, T79c0ff0Tb4b4b4, T79c0ff1T79c0ff5Tb4b4b4)

Tff7b72val Te6edf3rings Tff7b72= Te6edf3MvtDecoderTb4b4b4.Te6edf3decodeGeometryTb4b4b4(Te6edf3VectorTileTb4b4b4.Te6edf3GEOM_POLYGONTb4b4b4, Te6edf3commandsTb4b4b4)

Te6edf3assertEqualsTb4b4b4(
Te6edf3listOfTb4b4b4(Te6edf3TileCoordTb4b4b4(T79c0ff0Tb4b4b4, T79c0ff0Tb4b4b4)Tb4b4b4, Te6edf3TileCoordTb4b4b4(T79c0ff1T79c0ff0Tb4b4b4, T79c0ff0Tb4b4b4)Tb4b4b4, Te6edf3TileCoordTb4b4b4(T79c0ff1T79c0ff0Tb4b4b4, T79c0ff1T79c0ff0Tb4b4b4)Tb4b4b4, Te6edf3TileCoordTb4b4b4(T79c0ff0Tb4b4b4, T79c0ff1T79c0ff0Tb4b4b4)Tb4b4b4, Te6edf3TileCoordTb4b4b4(T79c0ff0Tb4b4b4, T79c0ff0Tb4b4b4)Tb4b4b4)Tb4b4b4,
Te6edf3ringsTb4b4b4.Te6edf3singleTb4b4b4(Tb4b4b4)Tb4b4b4,
Tb4b4b4)
Tb4b4b4}

Tf0883e@Test
Tff7b72fun Td2a8ff`a MultiPoint feature's single MoveTo command yields one point per delta`Tb4b4b4(Tb4b4b4) Tb4b4b4{
T8b949e// MoveTo(count=3): (5,5), then +(-2,0), then +(0,3) — three independent points, not a connected line.
Tff7b72val Te6edf3commands Tff7b72= Te6edf3listOfTb4b4b4(Tb4b4b4(T79c0ff1Tb4b4b4) Te6edf3or Tb4b4b4(T79c0ff3 Te6edf3shl T79c0ff3Tb4b4b4)Tb4b4b4, T79c0ff1T79c0ff0Tb4b4b4, T79c0ff1T79c0ff0Tb4b4b4, T79c0ff3Tb4b4b4, T79c0ff0Tb4b4b4, T79c0ff0Tb4b4b4, T79c0ff6Tb4b4b4)

Tff7b72val Te6edf3points Tff7b72= Te6edf3MvtDecoderTb4b4b4.Te6edf3decodeGeometryTb4b4b4(Te6edf3VectorTileTb4b4b4.Te6edf3GEOM_POINTTb4b4b4, Te6edf3commandsTb4b4b4)

Te6edf3assertEqualsTb4b4b4(Te6edf3listOfTb4b4b4(Te6edf3TileCoordTb4b4b4(T79c0ff5Tb4b4b4, T79c0ff5Tb4b4b4)Tb4b4b4, Te6edf3TileCoordTb4b4b4(T79c0ff3Tb4b4b4, T79c0ff5Tb4b4b4)Tb4b4b4, Te6edf3TileCoordTb4b4b4(T79c0ff3Tb4b4b4, T79c0ff8Tb4b4b4)Tb4b4b4)Tb4b4b4, Te6edf3pointsTb4b4b4.Te6edf3singleTb4b4b4(Tb4b4b4)Tb4b4b4)
Tb4b4b4}

Tf0883e@Test
Tff7b72fun Td2a8ff`an open LineString is returned without an implicit close`Tb4b4b4(Tb4b4b4) Tb4b4b4{
T8b949e// MoveTo(1) to (0,0), LineTo(2) to (4,0) then (4,4) — no ClosePath, so no return-to-start point.
Tff7b72val Te6edf3commands Tff7b72= Te6edf3listOfTb4b4b4(T79c0ff9Tb4b4b4, T79c0ff0Tb4b4b4, T79c0ff0Tb4b4b4, Tb4b4b4(T79c0ff2Tb4b4b4) Te6edf3or Tb4b4b4(T79c0ff2 Te6edf3shl T79c0ff3Tb4b4b4)Tb4b4b4, T79c0ff8Tb4b4b4, T79c0ff0Tb4b4b4, T79c0ff0Tb4b4b4, T79c0ff8Tb4b4b4)

Tff7b72val Te6edf3lines Tff7b72= Te6edf3MvtDecoderTb4b4b4.Te6edf3decodeGeometryTb4b4b4(Te6edf3VectorTileTb4b4b4.Te6edf3GEOM_LINESTRINGTb4b4b4, Te6edf3commandsTb4b4b4)

Te6edf3assertEqualsTb4b4b4(Te6edf3listOfTb4b4b4(Te6edf3TileCoordTb4b4b4(T79c0ff0Tb4b4b4, T79c0ff0Tb4b4b4)Tb4b4b4, Te6edf3TileCoordTb4b4b4(T79c0ff4Tb4b4b4, T79c0ff0Tb4b4b4)Tb4b4b4, Te6edf3TileCoordTb4b4b4(T79c0ff4Tb4b4b4, T79c0ff4Tb4b4b4)Tb4b4b4)Tb4b4b4, Te6edf3linesTb4b4b4.Te6edf3singleTb4b4b4(Tb4b4b4)Tb4b4b4)
Tb4b4b4}

Tf0883e@Test
Tff7b72fun Td2a8ff`a truncated parameter stream drops the rest of the tile instead of spinning through count`Tb4b4b4(Tb4b4b4) Tb4b4b4{
T8b949e// LineTo(count=1) with no dx,dy pair following it — truncated immediately. A malformed/malicious tile can
T8b949e// set count up to ~536M; the fix must detect this in O(1), not by exhausting `count` iterations.
Tff7b72val Te6edf3commands Tff7b72= Te6edf3listOfTb4b4b4(Tb4b4b4(T79c0ff2Tb4b4b4) Te6edf3or Tb4b4b4(T79c0ff1 Te6edf3shl T79c0ff3Tb4b4b4)Tb4b4b4)

Tff7b72val Te6edf3lines Tff7b72= Te6edf3MvtDecoderTb4b4b4.Te6edf3decodeGeometryTb4b4b4(Te6edf3VectorTileTb4b4b4.Te6edf3GEOM_LINESTRINGTb4b4b4, Te6edf3commandsTb4b4b4)

Te6edf3assertEqualsTb4b4b4(Te6edf3emptyListTb4b4b4(Tb4b4b4)Tb4b4b4, Te6edf3linesTb4b4b4)
Tb4b4b4}

Tf0883e@Test
Tff7b72fun Td2a8ff`a truncated command near the end of the stream is not misread as a trailing command`Tb4b4b4(Tb4b4b4) Tb4b4b4{
T8b949e// MoveTo(0,0), LineTo(5,5) [both valid], then a truncated LineTo(count=1) header with no dx,dy pair behind
T8b949e// it, followed by one leftover int that would decode as a valid ClosePath(count=1) header if the parser
T8b949e// kept going after the truncation. The old repeat+return@repeat implementation only skipped the failing
T8b949e// iteration, not the whole command stream, so the outer loop moved on and spuriously closed the ring — this
T8b949e// pins that the ring comes back open (2 points, not 3) once truncation correctly abandons the whole stream.
Tff7b72val Te6edf3moveTo Tff7b72= Te6edf3listOfTb4b4b4(T79c0ff9Tb4b4b4, T79c0ff0Tb4b4b4, T79c0ff0Tb4b4b4) T8b949e// MoveTo(count=1) -> (0,0)
Tff7b72val Te6edf3lineTo Tff7b72= Te6edf3listOfTb4b4b4(T79c0ff1T79c0ff0Tb4b4b4, T79c0ff1T79c0ff0Tb4b4b4, T79c0ff1T79c0ff0Tb4b4b4) T8b949e// LineTo(count=1) -> +(5,5) = (5,5)
Tff7b72val Te6edf3truncatedLineTo Tff7b72= T79c0ff1T79c0ff0 T8b949e// LineTo(count=1) header with no dx,dy pair following
Tff7b72val Te6edf3leftoverClosePathHeader Tff7b72= Tb4b4b4(T79c0ff1 Te6edf3shl T79c0ff3Tb4b4b4) Te6edf3or T79c0ff7 T8b949e// would decode as ClosePath(count=1) if read as a new header
Tff7b72val Te6edf3commands Tff7b72= Te6edf3moveTo Tff7b72+ Te6edf3lineTo Tff7b72+ Te6edf3truncatedLineTo Tff7b72+ Te6edf3leftoverClosePathHeader

Tff7b72val Te6edf3rings Tff7b72= Te6edf3MvtDecoderTb4b4b4.Te6edf3decodeGeometryTb4b4b4(Te6edf3VectorTileTb4b4b4.Te6edf3GEOM_POLYGONTb4b4b4, Te6edf3commandsTb4b4b4)

Te6edf3assertEqualsTb4b4b4(Te6edf3listOfTb4b4b4(Te6edf3TileCoordTb4b4b4(T79c0ff0Tb4b4b4, T79c0ff0Tb4b4b4)Tb4b4b4, Te6edf3TileCoordTb4b4b4(T79c0ff5Tb4b4b4, T79c0ff5Tb4b4b4)Tb4b4b4)Tb4b4b4, Te6edf3ringsTb4b4b4.Te6edf3singleTb4b4b4(Tb4b4b4)Tb4b4b4)
Tb4b4b4}
Tb4b4b4}

Served by rngit 1.5.2 - Generated in 0.06s